using System;
using System.Web;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web.Security;


namespace newtelligence.DasBlog.Web.Services
{
    /// <summary>
    /// Summary description for DeleteItemHandler.
    /// </summary>
    public partial class WebWebServices
    {
        [OperationContract, WebInvoke(Method = "POST", UriTemplate="/deleteItem?entryId={entryId}&commentId={commentId}&referralPermalink={referralPermalink}&type={type}&report={report}")]
        public void DeleteItemHandler(
            string entryId,
            string commentId,
            string referralPermalink,
            string type,
            string report)
        {
            
            if (!(Roles.IsUserInRole("admin") || Roles.IsUserInRole("contributor")))
            {
                WebOperationContext.Current.OutgoingResponse.Location = Utils.GetStartPageUrl(siteConfig);
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
                WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
                return;
            }

            string redirectUrl = Utils.GetStartPageUrl();
            bool reportAsSpam;
            reportAsSpam = string.Compare(report, "true",true)==0;

            // make sure the entry param is there
            if (entryId == null || entryId.Length == 0)
            {
                redirectUrl = Utils.GetStartPageUrl(siteConfig);
            }
            else
            {
                try
                {
                    Entry entry = dataService.GetEntry(entryId);
                    if (entry != null)
                    {
                        if (commentId != null && commentId.Length > 0)
                        {
                            if (reportAsSpam)
                            {
                                ISpamBlockingService spamBlockingService = siteConfig.SpamBlockingService;
                                if (spamBlockingService != null)
                                {
                                    Comment comment = dataService.GetCommentById(entryId, commentId);
                                    if ((comment != null) && (comment.SpamState != SpamState.Spam))
                                    {
                                        try
                                        {
                                            spamBlockingService.ReportSpam(comment);
                                        }
                                        catch (Exception ex)
                                        {
                                            loggingService.AddEvent(new EventDataItem(EventCodes.Error, String.Format("Unable to report comment {0} as spam. Original exception: {1}", comment.EntryId, ex), Utils.GetPermaLinkUrl(entryId)));
                                        }
                                    }
                                }
                            }
                            dataService.DeleteComment(entryId, commentId);

                            loggingService.AddEvent(
                                new EventDataItem(
                                EventCodes.CommentDeleted, commentId,
                                Utils.GetPermaLinkUrl(entryId)));

                            redirectUrl = Utils.GetCommentViewUrl(entryId);
                        }
                        else if (referralPermalink != null && referralPermalink.Length > 0)
                        {
                            TrackingType trackingType = TrackingType.Referral;

                            if (type != null && type.Length != 0)
                            {
                                trackingType = (TrackingType)Enum.Parse(typeof(TrackingType), type);
                            }

                            dataService.DeleteTracking(entryId, referralPermalink, trackingType);

                            loggingService.AddEvent(
                                new EventDataItem(
                                EventCodes.ItemReferralDeleted, referralPermalink,
                                Utils.GetPermaLinkUrl(entryId)));

                            redirectUrl = Utils.GetPermaLinkUrl(entryId);
                        }
                        else // it must be an entry we are deleting
                        {
                            Utils.DeleteEntry(entryId, siteConfig, loggingService, dataService);
                            redirectUrl = Utils.GetStartPageUrl();
                        }
                    }
                }
                catch (Exception exc)
                {
                    // absorb
                    ErrorTrace.Trace(System.Diagnostics.TraceLevel.Error, exc);
                }
            }

            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
            WebOperationContext.Current.OutgoingResponse.Location = redirectUrl;
        }
    }
}
